<?php
namespace application\admin\controller;
use application\admin\model\User as UserModel;
use application\admin\model\AuthGroup;
use application\admin\model\AuthGroupAccess;

class User extends Base
{

    // 用户列表
    public function lists()
    {
        $mUser = new UserModel();
        $list = $mUser->getList();
        
        $this->assign('list', $list);
        return $this->fetch();
    }

    // 添加用户
    public function add()
    {
        if( $this->request->isPost() )
        {
            $sPost = $this->request->post();
            if($sPost['name'] && $sPost['password'] && $sPost['email'])
            {
                if($sPost['group_id'])
                {
                    $mUser = new UserModel();
                    $params = [];
                    $params['name'] = $sPost['name'];
                    $params['password'] = md5($sPost['password']);
                    $params['email'] = $sPost['email'];
                    $params['mobile'] = $sPost['mobile'];
                    $params['nickname'] = $sPost['nickname'];
                    $result = $mUser->addItem($params);

                    if($result['status'] == 0){
                        // 添加用户到角色组
                        $mAuthGroupAccess = new AuthGroupAccess();
                        $mAuthGroupAccess->setItem($result['value'], $sPost['group_id']);
                        
                        return ['status'=>0, 'msg'=>'success'];
                    }else{
                        return ['status'=>1, 'msg'=>$result['msg']];
                    }
                }else{
                    return ['status'=>1, 'msg'=>'请选择用户角色组！'];
                }
            }else{
                return ['status'=>1, 'msg'=>'用户名、密码、邮箱必须填写'];
            }
            
        }else{
            $mAuthGroup = new AuthGroup();
            $sGroupList = $mAuthGroup->getList();
            $this->assign('auth_group_list', $sGroupList);
            
            return $this->fetch();
        }
    }

    // 修改用户
    public function edit()
    {
        if( $this->request->isPost() )
        {
            
            
            
        }else{
            return $this->fetch();
        }
    }

}